using Maticsoft.DBUtility;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace uTrade.Data
{
    public class StockCwInfoManager
    {
        public StockCwInfoManager()
        { }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public static int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Symbol", "tbl_Stock_CwInfo");
        }

        /// <summary>
        /// 清空
        /// </summary>
        public static int Clear()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_Stock_CwInfo ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            return rows;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(string symbol)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbl_Stock_CwInfo");
            strSql.Append(" where Symbol=@symbol");
            SqlParameter[] parameters = {
                    new SqlParameter("@symbol", SqlDbType.VarChar ,10)
            };
            parameters[0].Value = symbol;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static int Add(StockCwInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbl_Stock_CwInfo(");
            strSql.Append("Symbol, ReportDate, JBMGSY, MGJZC, MGJYHDCSXJLJE, ZYYWSR, ZYYWLR, YYLR, TZSY, YYEYSZJE, LRZE, JLR, JLROUT, JYHDCSDXJLJE, XJJXJDJWJCJE, ZZC, LDZC, ZFZ, LDFZ, GDQYBHSSGDQY, JZCSYLJQ)");
            strSql.Append(" values (");
            strSql.Append("@Symbol, @ReportDate, @JBMGSY, @MGJZC, @MGJYHDCSXJLJE, @ZYYWSR, @ZYYWLR, @YYLR, @TZSY, @YYEYSZJE, @LRZE, @JLR, @JLROUT, @JYHDCSDXJLJE, @XJJXJDJWJCJE, @ZZC, @LDZC, @ZFZ, @LDFZ, @GDQYBHSSGDQY, @JZCSYLJQ)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@Symbol", SqlDbType.VarChar ,10),
new SqlParameter("@ReportDate", SqlDbType.DateTime),
new SqlParameter("@JBMGSY", SqlDbType.Decimal ,9),
new SqlParameter("@MGJZC", SqlDbType.Decimal ,9),
new SqlParameter("@MGJYHDCSXJLJE", SqlDbType.Decimal ,9),
new SqlParameter("@ZYYWSR", SqlDbType.NVarChar ,50),
new SqlParameter("@ZYYWLR", SqlDbType.NVarChar ,50),
new SqlParameter("@YYLR", SqlDbType.NVarChar ,50),
new SqlParameter("@TZSY", SqlDbType.NVarChar ,50),
new SqlParameter("@YYEYSZJE", SqlDbType.NVarChar ,50),
new SqlParameter("@LRZE", SqlDbType.NVarChar ,50),
new SqlParameter("@JLR", SqlDbType.NVarChar ,50),
new SqlParameter("@JLROUT", SqlDbType.NVarChar ,50),
new SqlParameter("@JYHDCSDXJLJE", SqlDbType.NVarChar ,50),
new SqlParameter("@XJJXJDJWJCJE", SqlDbType.NVarChar ,50),
new SqlParameter("@ZZC", SqlDbType.NVarChar ,50),
new SqlParameter("@LDZC", SqlDbType.NVarChar ,50),
new SqlParameter("@ZFZ", SqlDbType.NVarChar ,50),
new SqlParameter("@LDFZ", SqlDbType.NVarChar ,50),
new SqlParameter("@GDQYBHSSGDQY", SqlDbType.NVarChar ,50),
new SqlParameter("@JZCSYLJQ", SqlDbType.Decimal ,9)};
            parameters[0].Value = model.Symbol;
            parameters[1].Value = model.ReportDate;
            parameters[2].Value = model.JBMGSY;
            parameters[3].Value = model.MGJZC;
            parameters[4].Value = model.MGJYHDCSXJLJE;
            parameters[5].Value = model.ZYYWSR;
            parameters[6].Value = model.ZYYWLR;
            parameters[7].Value = model.YYLR;
            parameters[8].Value = model.TZSY;
            parameters[9].Value = model.YYEYSZJE;
            parameters[10].Value = model.LRZE;
            parameters[11].Value = model.JLR;
            parameters[12].Value = model.JLROUT;
            parameters[13].Value = model.JYHDCSDXJLJE;
            parameters[14].Value = model.XJJXJDJWJCJE;
            parameters[15].Value = model.ZZC;
            parameters[16].Value = model.LDZC;
            parameters[17].Value = model.ZFZ;
            parameters[18].Value = model.LDFZ;
            parameters[19].Value = model.GDQYBHSSGDQY;
            parameters[20].Value = model.JZCSYLJQ;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static int Update(StockCwInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbl_Stock_CwInfo set ");
            strSql.Append("ReportDate = @ReportDate,");
            strSql.Append("JBMGSY = @JBMGSY,");
            strSql.Append("MGJZC = @MGJZC,");
            strSql.Append("MGJYHDCSXJLJE = @MGJYHDCSXJLJE,");
            strSql.Append("ZYYWSR = @ZYYWSR,");
            strSql.Append("ZYYWLR = @ZYYWLR,");
            strSql.Append("YYLR = @YYLR,");
            strSql.Append("TZSY = @TZSY,");
            strSql.Append("YYEYSZJE = @YYEYSZJE,");
            strSql.Append("LRZE = @LRZE,");
            strSql.Append("JLR = @JLR,");
            strSql.Append("JLROUT = @JLROUT,");
            strSql.Append("JYHDCSDXJLJE = @JYHDCSDXJLJE,");
            strSql.Append("XJJXJDJWJCJE = @XJJXJDJWJCJE,");
            strSql.Append("ZZC = @ZZC,");
            strSql.Append("LDZC = @LDZC,");
            strSql.Append("ZFZ = @ZFZ,");
            strSql.Append("LDFZ = @LDFZ,");
            strSql.Append("GDQYBHSSGDQY = @GDQYBHSSGDQY,");
            strSql.Append("JZCSYLJQ = @JZCSYLJQ,");

            strSql.Append(" where Symbol=@symbol");
            SqlParameter[] parameters = {
                    new SqlParameter("@Symbol", SqlDbType.VarChar ,10),
new SqlParameter("@ReportDate", SqlDbType.DateTime),
new SqlParameter("@JBMGSY", SqlDbType.Decimal ,9),
new SqlParameter("@MGJZC", SqlDbType.Decimal ,9),
new SqlParameter("@MGJYHDCSXJLJE", SqlDbType.Decimal ,9),
new SqlParameter("@ZYYWSR", SqlDbType.NVarChar ,50),
new SqlParameter("@ZYYWLR", SqlDbType.NVarChar ,50),
new SqlParameter("@YYLR", SqlDbType.NVarChar ,50),
new SqlParameter("@TZSY", SqlDbType.NVarChar ,50),
new SqlParameter("@YYEYSZJE", SqlDbType.NVarChar ,50),
new SqlParameter("@LRZE", SqlDbType.NVarChar ,50),
new SqlParameter("@JLR", SqlDbType.NVarChar ,50),
new SqlParameter("@JLROUT", SqlDbType.NVarChar ,50),
new SqlParameter("@JYHDCSDXJLJE", SqlDbType.NVarChar ,50),
new SqlParameter("@XJJXJDJWJCJE", SqlDbType.NVarChar ,50),
new SqlParameter("@ZZC", SqlDbType.NVarChar ,50),
new SqlParameter("@LDZC", SqlDbType.NVarChar ,50),
new SqlParameter("@ZFZ", SqlDbType.NVarChar ,50),
new SqlParameter("@LDFZ", SqlDbType.NVarChar ,50),
new SqlParameter("@GDQYBHSSGDQY", SqlDbType.NVarChar ,50),
new SqlParameter("@JZCSYLJQ", SqlDbType.Decimal ,9)};
            parameters[0].Value = model.Symbol;
            parameters[1].Value = model.ReportDate;
            parameters[2].Value = model.JBMGSY;
            parameters[3].Value = model.MGJZC;
            parameters[4].Value = model.MGJYHDCSXJLJE;
            parameters[5].Value = model.ZYYWSR;
            parameters[6].Value = model.ZYYWLR;
            parameters[7].Value = model.YYLR;
            parameters[8].Value = model.TZSY;
            parameters[9].Value = model.YYEYSZJE;
            parameters[10].Value = model.LRZE;
            parameters[11].Value = model.JLR;
            parameters[12].Value = model.JLROUT;
            parameters[13].Value = model.JYHDCSDXJLJE;
            parameters[14].Value = model.XJJXJDJWJCJE;
            parameters[15].Value = model.ZZC;
            parameters[16].Value = model.LDZC;
            parameters[17].Value = model.ZFZ;
            parameters[18].Value = model.LDFZ;
            parameters[19].Value = model.GDQYBHSSGDQY;
            parameters[20].Value = model.JZCSYLJQ;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static bool Delete(string symbol)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_Stock_CwInfo ");
            strSql.Append(" where Symbol=@symbol");
            SqlParameter[] parameters = {
                    new SqlParameter("@symbol", SqlDbType.VarChar ,10)
            };
            parameters[0].Value = symbol;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public static int DeleteList(string symbollist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_Stock_CwInfo ");
            strSql.Append(" where Symbol in (" + symbollist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            return rows;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static StockCwInfo GetModel(string symbol)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Symbol, ReportDate, JBMGSY, MGJZC, MGJYHDCSXJLJE, ZYYWSR, ZYYWLR, YYLR, TZSY, YYEYSZJE, LRZE, JLR, JLROUT, JYHDCSDXJLJE, XJJXJDJWJCJE, ZZC, LDZC, ZFZ, LDFZ, GDQYBHSSGDQY, JZCSYLJQ from tbl_Stock_CwInfo ");
            strSql.Append(" where Symbol=@symbol");
            SqlParameter[] parameters = {
                    new SqlParameter("@symbol", SqlDbType.VarChar ,10)
            };
            parameters[0].Value = symbol;

            StockCwInfo model = new StockCwInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ModelConvertHelper<StockCwInfo>.ConvertToModel(ds.Tables[0])?[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Symbol, ReportDate, JBMGSY, MGJZC, MGJYHDCSXJLJE, ZYYWSR, ZYYWLR, YYLR, TZSY, YYEYSZJE, LRZE, JLR, JLROUT, JYHDCSDXJLJE, XJJXJDJWJCJE, ZZC, LDZC, ZFZ, LDFZ, GDQYBHSSGDQY, JZCSYLJQ ");
            strSql.Append(" FROM tbl_Stock_CwInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public static DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Symbol, ReportDate, JBMGSY, MGJZC, MGJYHDCSXJLJE, ZYYWSR, ZYYWLR, YYLR, TZSY, YYEYSZJE, LRZE, JLR, JLROUT, JYHDCSDXJLJE, XJJXJDJWJCJE, ZZC, LDZC, ZFZ, LDFZ, GDQYBHSSGDQY, JZCSYLJQ ");
            strSql.Append(" FROM tbl_Stock_CwInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public static int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbl_Stock_CwInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Symbol desc");
            }
            strSql.Append(")AS Row, T.*  from tbl_Stock_CwInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbl_Stock_CwInfo";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
    }
}